<template>
  <div v-loading="loading" element-loading-text="加载中。。。">
    <el-card style="overflow:visible;">
      <el-form  :model="form" size="mini" :rules="rules" label-position="top" ref="form">
        <el-row :gutter="10">
          <el-col :lg="4" :md="6">
            <el-form-item label='公司' prop='company'>
              <el-select v-model="form.company" placeholder="请选择公司" @change="queryTeam">
                <el-option 
                key="index"
                label="全部"
                value="">
                </el-option>
                <el-option v-for="item in companyList" 
                :key="item.TEAMID"
                :label="(item.TEAMID+'-'+item.TEAMNAME)"
                :value="item.TEAMID">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
           <el-col :md="6" :lg="4">
            <el-form-item label="班组" prop="team">
              <el-select v-model="form.team" filterable placeholder="请选择班组" @change="agentQuery">
                <el-option 
                key="index"
                label="全部"
                value="">
                </el-option>
                <el-option
                  v-for="item in teamList"
                  :key="item.TEAMID"
                  :label="(item.TEAMID+'-'+item.TEAMNAME)"
                  :value="item.TEAMID"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="6" :lg="4">
            <el-form-item label="坐席" prop="agent">
              <el-select v-model="form.agent" multiple placeholder="请选择坐席">
                <el-option
                  v-for="item in agentList"
                  :key="item.AGENTID"
                  :label="(item.AGENTID+'-'+item.AGENTNAME)"
                  :value="item.AGENTID"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="6" :lg="4">
              <el-form-item label='日期' class='typeInfo-el-form-item' prop='startdate'>
                <el-date-picker v-model="form.startdate" value-format="yyyyMMdd" type="date" placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <!-- <el-col :md="6" :lg="4">
              <el-form-item label='结束日期' class='typeInfo-el-form-item' prop='enddate'>
                <el-date-picker v-model="form.enddate" value-format="yyyyMMdd" type="date" placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col> -->
          <el-col :lg="2" :md="3"  class="searchBtnMarginTop">
            <el-button type="primary" icon="el-icon-search" :loading="loading" size="mini" @click="query(1)">查询</el-button>
          </el-col>
          <el-col :md="3" :lg="2" class="searchBtnMarginTop">
            <el-button type='warning' size='mini' icon='el-icon-refresh' @click="clearForm('form')">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card style="margin-top:10px;">
      <el-row :gutter="10">
        <el-col :lg="2" :md="3" v-if="btnPermission('Button_User_Download')">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-download"
            :load="this.download"
            :disabled="tableData.length==0"
            @click="getExcel"
          >导出</el-button>
        </el-col>
      </el-row>
      <el-table
        :data="tableData"
        ref="table"
        border
        class="tableMarginTop"
        header-row-class-name="tableHeaderClass"
        highlight-current-row
        @selection-change="rowDate">
        <el-table-column type="selection" width="40px"></el-table-column>
        <el-table-column prop="AGENTID" label="坐席工号"></el-table-column>
        <el-table-column prop="AGENTNAME" label="坐席姓名"></el-table-column>
        <el-table-column prop="DDATE" label="时间"></el-table-column>
        <el-table-column prop="NVOICE_AGENTLOGIN" label="签入次数"></el-table-column>
        <el-table-column prop="JTIME" label="工作时长"></el-table-column>
        <el-table-column prop="DTIME_IN_VOICE" label="电话呼入时长"></el-table-column>
        <el-table-column prop="DYDDHL_VOICE" label="电话呼入接通量"></el-table-column>
        <el-table-column prop="DYDDHL_OUT" label="电话呼出次数"></el-table-column>
        <el-table-column prop="DTIME_OUTBOUND" label="电话呼出时长"></el-table-column>
        <el-table-column prop="TOTALNUM_ACW" label="话后小结次数"></el-table-column>
        <el-table-column prop="TOTAL_ACW_TIME" label="话后小结时长"></el-table-column>
        <el-table-column prop="NVOICE_BREAK" label="电话小休次数"></el-table-column>
        <el-table-column prop="TVOICE_BREAK" label="电话小休时长"></el-table-column>
        <el-table-column prop="BNUM" label="保持次数"></el-table-column>
        <el-table-column prop="GTIME" label="保持时长"></el-table-column>
        <el-table-column prop="NVOICE_NOTREADY" label="电话示忙次数"></el-table-column>
        <el-table-column prop="TVOICE_NOTREADY" label="电话示忙时长"></el-table-column>
        <el-table-column prop="TOTALTIME_DELIVERI" label="振铃时长"></el-table-column>
        <el-table-column prop="ITIME" label="就绪时长"></el-table-column>
        <el-table-column prop="EFFICIENT" label="工时利用率(含整理时长)"></el-table-column>
        <el-table-column prop="EFFICIENT2" label="工时利用率"></el-table-column>
        <el-table-column prop="ANUM" label="转接次数"></el-table-column>
      </el-table>
      <div style="text-align:right;margin-top:20px;" v-show="this.tableData">
        <el-pagination
          @size-change="handleSizeChange"
          :page-sizes="[10,25,50,100]"
          :page-size="pageSize"
          @current-change="changePage"
          :current-page="currentPage"
          layout="total,sizes, prev, pager, next, jumper"
          background
          :total="totalRcrdNum"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import toolBox from "@/common/utils/toolBox";
import comQuery from "@/message/siteManagement/talkerContinue/comQuery";
import query from "@/message/siteManagement/talkerContinue/query";
import request1 from "@/common/utils/requestTest";
import request from "@/common/utils/requestYg";
import getGlobalParams from "@/common/utils/getGlobalParams";
import agentQuery from "@/message/siteManagement/talkerContinue/agentQuery";
import getExcel from "@/message/siteManagement/talkerContinue/getExcel";
export default {
  name: "",
  components: {},
  data() {
    return {
      download:false,
      pageSize: undefined, //表格每页的条数
      form: {
        //表单数据
        company:"",
        team: "",
        agent: [],
        startdate: "",
        enddate: ""
      },
      rules:{
        startdate: [{ 
          required: true, 
          message: "开始日期不能为空", 
          trigger: "blur" 
        }]
      },
      list1: [],
      filename: "",
      fileUrl: getGlobalParams.get("agentDownloadAddress"),
      agentList: [],
      teamList: [],
      companyList: [],
      currentRow: [], //表格当前选中行
      currentPage: 1, //表格当前页
      totalRcrdNum: 0,
      tableData: [], //表格 数据
      loading: false, //loading遮罩
    };
  },
  computed: {},
  created: function() {
    this.pageSize = getGlobalParams.get("PageSize");
    this.form.startdate = toolBox.getNowTime(0);
    // this.query();
    this.comQuery();
  },
   watch: {
  },
  methods: {
    comQuery(){
      //公司查询
      let resBody = new comQuery();
      resBody.data.TEAMTYPE = "1";
      request1(resBody)
        .then(response => {
          if (response.SYS_HEAD.ReturnCode === "000000") {
            this.companyList = response.RSP_BODY.RspStruct || [];
          } else {
            this.$message({
              type: "error",
              message: response.SYS_HEAD.ReturnMessage
            });
            this.companyList=[];
          }
          this.loading = false;
        })
        .catch(error => {
          this.loading = false;
        })
    
    },
    //查询坐席
    agentQuery(val){
      this.agentList = [];
      this.form.agent = [];
      let resBody = new agentQuery();
      resBody.data.agentdep = this.form.company;
      resBody.data.agentteam = val;
      request(resBody)
        .then(response => {
          if (response.SYS_HEAD.ReturnCode === "000000") {
            this.agentList = response.RSP_BODY.AgentList || [];
          } else {
            this.$message({
              type: "error",
              message: response.SYS_HEAD.ReturnMessage
            });
            this.agentList=[];
          }
          this.loading = false;
        })
        .catch(error => {
          this.loading = false;
        })
    
    },
    //数据模板ID初始化下拉框
     queryTeam(val){
       if(val === "") {
         this.teamList = [];
         this.agentList = [];
         this.form.agent = [];
         this.form.team = "";
       } else {

         this.teamList = [];
         this.agentList = [];
         this.form.agent = [];
         this.form.team = "";
         let resBody = new comQuery();
         resBody.data.TEAMID = '';
         resBody.data.TEAMNAME = '';
         resBody.data.TEAMLEVEL= '';
         resBody.data.TEAMTYPE = '0';
         resBody.data.HTEAMID = val;
         resBody.data.pageflag = 4;
         resBody.data.pagerownum = 999999;
         resBody.data.currpage = this.currentPage;
         request1(resBody)
           .then(response => {
             if (response.SYS_HEAD.ReturnCode === "000000") {
               this.teamList = response.RSP_BODY.RspStruct || [];
               this.agentQuery("");
             } else {
               this.$message({
                 type: "error",
                 message: response.SYS_HEAD.ReturnMessage
               });
               this.teamList=[];
             }
           })
           .catch(error => {
           })
       }
    
    },
    query(num){
      this.$refs["form"].validate(valid => {
        if(valid) {
          this.loading = true;
          this.currentPage = num;
          let resBody = new query();
          let list = "";
          if(this.form.agent != null) {
              for(let i = 0; i < this.form.agent.length; i++) {
                if(i == 0) {
                  list ="'" + this.form.agent[i] + "'";
                } else {
                  list = list+ "," + "'" + this.form.agent[i] + "'";
                }
              }
              // console.log("=============", list);
            }
          resBody.data.company=this.form.company,
          resBody.data.group=this.form.team,
          resBody.data.agentid = list;
          resBody.data.begindate = this.form.startdate;
          // resBody.data.enddate = this.form.enddate;
          resBody.data.pageflag = 4;
          resBody.data.currpage = this.currentPage;
          resBody.data.pagerownum = this.pageSize;
          request(resBody)
            .then(response => {
            console.log("返回信息"+response)
              if (response.SYS_HEAD.ReturnCode === "000000") {
                this.tableData = response.RSP_BODY.AgentList || [];
                this.totalRcrdNum = response.RSP_BODY.TotalNum;
              } else {
                this.$message({
                  type: "error",
                  message: response.SYS_HEAD.ReturnMessage
                });
                this.tableData=[];
              }
              this.loading = false;
            })
            .catch(error => {
              this.loading = false;
            })
          } else {
              this.$message({
                type: "warning",
                message: "请输入合法的数据！"
              });
              return false;
          }
        });
    },
    downFile(){
      this.download = false;
      if(this.filename.length==0){
        this.$message({
            type: "error",
            message: `文件不存在，无法下载`
        });

      }else{
        window.location.href = this.fileUrl + this.filename;
      }
    },
    getExcel(){
      this.$refs["form"].validate(valid => {
        if(valid) {
          this.$message({
            type: "success",
            message: `正在下载。。。。。。`
          }); 
          this.download = true;
          let resBody = new getExcel();
          let list = "";
          if(this.form.agentid != null) {
              for(let i = 0; i < this.form.agentid.length; i++) {
                if(i == 0) {
                  list ="'" + this.form.agentid[i] + "'";
                } else {
                  list = list+ "," + "'" + this.form.agentid[i] + "'";
                }
              }
              // console.log("=============", list);
          }
          resBody.data.company = this.form.company;
          resBody.data.group=this.form.team,
          resBody.data.agentid = list;
          resBody.data.begindate = this.form.startdate;
          resBody.data.enddate = this.form.enddate;
          resBody.data.code = "Y018";

          request(resBody)
          .then(response => {
            if(response.SYS_HEAD.ReturnCode === "000000"){
              this.filename = response.RSP_BODY.filepath;
              console.log("this.filename", this.filename);
              this.downFile()
            } else {
              this.$message({
                type: "error",
                message: response.SYS_HEAD.ReturnMessage
              });
            }
            this.loading = false;
          })
          .catch(error => {
            this.loading = false;
          })
        } else {
          this.$message({
            type: "warning",
            message: "请输入合法的数据！"
          });
          return false;
        }
      });
    },
     // 重置查询条件
    clearForm(item) {
      this.$refs["" + item].resetFields();
      this.form.startdate = toolBox.getNowTime(0);
      this.form.enddate = toolBox.getNowTime(0);
    },
    changePage(val) {
      // this.currentPage = val;
      this.query(val);
    },
    handleSizeChange(val){
      this.pageSize = val;
      this.query(1);
    },
    rowDate(val) {
      this.currentRow = val;
    },
    refreshTable() {
      //刷新数据并清除选中的数据
      this.query(1);
    },
  }
};
</script>


<style lang="scss" scoped>
.selectTree {
  min-width: 150px;
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}
.dialog-text {
  display: inline-block;
  width: 100%;
  text-align: center;
  color: red;
}       
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.el-card {
  width: 100%;
}
.el-form-item {
  margin-left: 30px;
}
.el-form-item1 {
  margin-left: 10px;
}
.box2-button {
  width: 80px;
}

.el-card {
  overflow: inherit;
}
</style>
